#pragma once

#ifndef SAFE_DELETE
#define SAFE_DELETE(p) { if (p) delete (p); (p) = 0; }
#define SAFE_DELETE_ARR(p) { if (p) delete [] (p); (p) = 0; }
#endif

// do not accessible
class cFileData;

//-------------------------------------------------------------------------------------------------
// Basic class for working with files. Can read from ZIP.
//-------------------------------------------------------------------------------------------------

class cFile
{
public:
	//-------------------------------------------------------------------------------------------------
	// All errors, which can generate File Manager
	struct cError
	{
		enum {
			Ok = 0,				// no errors
			FileNotFound = 1,	// file not found on disk or in zip, or can't create on disk
			ZipNotFound = 2,	// zip file not found on disk
			WrongMode = 3,		// wrong mode (try to write in zip-file)
			DamageZip = 4,		// internal error in zip-file
			AlreadyOpen = 5		// try to open new file, before closing the old file
		};
	};

public:
	//-------------------------------------------------------------------
	// constructors and destructor
	cFile() : m_pData(0) {}
	~cFile() { if ( IsOpen() ) Close(); }

public:
	//-------------------------------------------------------------------
	// open file and close it.
	// szFileName - file name which we want to open
	// szZipName  -	can be 0. 
	//				if exist - file name of zip file, which contain our file,
	//				else our file located on disk
	// szMode - type of access permitted. look fopen(...) for more info.
	int Open( const char* szFileName, const char* szZipName, const char* szMode );
	int Close();

	// is file open?
	bool IsOpen();
	// return file size in bytes
	long GetSize();

	// read data from file. look fread(...) for more info.
	size_t Read(void* pBuffer, size_t uiSize, size_t uiCount);
	// write data in file (ONLY FOR FILES ON DISK). work like fwrite(...) function
	size_t Write(const void* pBuffer, size_t uiSize, size_t uiCount);

private:
	cFileData	*m_pData;	// type of file, access method and other info.
};